package com.blacksheep.mtm.service.Impl;

import com.blacksheep.mtm.common.exception.XmallException;
import com.blacksheep.mtm.common.utils.FileUtil;
import com.blacksheep.mtm.dao.TbCustomerMapper;
import com.blacksheep.mtm.pojo.common.DataTablesResult;
import com.blacksheep.mtm.pojo.manager.TbCustomer;
import com.blacksheep.mtm.service.CustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private TbCustomerMapper customerMapper;
    @Override
    public DataTablesResult getList(int draw, int start, int length, String search, String minDate, String maxDate, String orderCol, String orderDir) {
        DataTablesResult result=new DataTablesResult();
        //分页
        PageHelper.startPage(start/length+1,length);
        List<TbCustomer> list = customerMapper.selectList("%"+search+"%",minDate,maxDate,orderCol,orderDir);
        PageInfo<TbCustomer> pageInfo=new PageInfo<>(list);

//        EasyExcel.read("d:/1.xls",DemoData.class,new DemoDataListener(medicineMapper)).build().readAll();
        result.setRecordsFiltered((int)pageInfo.getTotal());
        result.setDraw(draw);
        result.setData(list);
        return null;
    }

    @Override
    public TbCustomer insert(TbCustomer customer) {
        if (customerMapper.insertSelective(customer)<1){
            throw  new XmallException("新增失败");
        }
        return customer;
    }

    @Override
    public TbCustomer updateCustomer(TbCustomer customer) {
        if (customerMapper.updateByPrimaryKeySelective(customer)<1){
            throw  new XmallException("更新失败");
        }
        return customer;
    }

    @Override
    @Transactional
    public void insertFromExcel(List<Object> sheetData1) {
        for (Object o:sheetData1){
            List<String> row =  (List<String>) o;
            String cardShop = row.get(0);
            String cardNo = row.get(1);
            String name = row.get(2);
            String sex = row.get(3);
            String phone = row.get(4);
            String province = row.get(5);
            String city = row.get(6);
            String country = row.get(7);
            String insuranceType = row.get(8);
            String Disease = row.get(9);
            String idNo = row.get(10);
            int sexId = (sex == "男"?1:0);
            TbCustomer customer = new TbCustomer(name,phone,"",1,cardNo,idNo,sexId,province,city,country,Disease,cardShop,insuranceType);
            if (customerMapper.insertSelective(customer)<1){
                throw  new XmallException("新增失败");
            }
        }
    }

    @Override
    public TbCustomer alertState(Integer id, Integer state) {
        TbCustomer customer = new TbCustomer();
        customer.setId(id);
        customer.setState(state);
        if (customerMapper.updateByPrimaryKeySelective(customer)<1){
            throw  new XmallException("更新失败");
        }
        return customer;
    }

    @Override
    public void download(HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        List<TbCustomer> customerList = customerMapper.selectAllList();
        for (TbCustomer customer:customerList){
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("ID",customer.getId());
            map.put("姓名",customer.getName());
            map.put("性别",customer.getName());
            map.put("手机",customer.getPhone());
            map.put("会员卡号",customer.getCardNo());
            map.put("省",customer.getAddProvince());
            map.put("市",customer.getAddCity());
            map.put("县",customer.getAddCounty());
            map.put("医保类型",customer.getInsuranceType());
            map.put("疾病",customer.getDisease());
            map.put("身份证号",customer.getIdentityNo());
            map.put("办卡门店",customer.getCardShop());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
